//===============企业准入-risk_management.js

//一级分类
const TypeEnum = {
    BASICINFO: 'BASICINFO',//基本信息
    QUALIFICATIONINFO: 'QUALIFICATIONINFO',//资质信息
    LEGALEXECUTIVE: 'LEGALEXECUTIVE',//法律行政
    PUBLICOPINION: 'PUBLICOPINION',//舆情信息
    PERIODICREVIEW: 'PERIODICREVIEW'//定期审查

}


/**
 * 二级分类
 *
 */
const SubCategoriesTypeEnum = {
    BRANDAUTHORIZATION: 'BRANDAUTHORIZATION',//品牌授权
    GUARANTEE: 'GUARANTEE',//保证书
    BUSINESSLICENSE: 'BUSINESSLICENSE',//营业执照
    CORPORATEINFO: 'CORPORATEINFO',//法人信息
    LEGALDISPUTES: 'LEGALDISPUTES',//涉案涉诉
    LABORARBITRATION: 'LABORARBITRATION',//劳动仲裁
    ADMINISTRATIVEPENALTY: 'ADMINISTRATIVEPENALTY',//行政处罚
    INTERNALFEEDBACK: 'INTERNALFEEDBACK',//内部反馈
    OFFICIALINFO: 'OFFICIALINFO',//官方信息
    SOCIETYINFO: 'SOCIETYINFO',//社会舆论
    EXAMINE: 'EXAMINE',//审查列表
    ORGINFO: 'ORGINFO',//组织信息
    RELATEDNESS: 'RELATEDNESS',//关联系调查
    BUSINESSACTIVITIES: 'BUSINESSACTIVITIES',//经营活动活动
    OTHER: 'OTHER'//其他

}

/**
 * 组织类型
 */
const orgInfoListType = {
    "FormList3": [TypeEnum.BASICINFO, SubCategoriesTypeEnum.RELATEDNESS],
    "FormList4": [TypeEnum.BASICINFO, SubCategoriesTypeEnum.BUSINESSACTIVITIES],
    "FormList5": [TypeEnum.QUALIFICATIONINFO, SubCategoriesTypeEnum.BRANDAUTHORIZATION],
    "FormList6": [TypeEnum.LEGALEXECUTIVE, SubCategoriesTypeEnum.LEGALDISPUTES],
    "FormList7": [TypeEnum.LEGALEXECUTIVE, SubCategoriesTypeEnum.ADMINISTRATIVEPENALTY],
    "FormList8": [TypeEnum.LEGALEXECUTIVE, SubCategoriesTypeEnum.LABORARBITRATION],
    "FormList9": [TypeEnum.PUBLICOPINION, SubCategoriesTypeEnum.INTERNALFEEDBACK],
    "FormList10": [TypeEnum.PUBLICOPINION, SubCategoriesTypeEnum.OFFICIALINFO],
    "FormList11": [TypeEnum.PUBLICOPINION, SubCategoriesTypeEnum.SOCIETYINFO],
    "FormList12": [TypeEnum.PERIODICREVIEW, SubCategoriesTypeEnum.EXAMINE]
}

/**
 * 一级页签切换tab 映射 panel
 */
const firstTabChangeList = {
    TabsPanelContainer3: "TabsPanelContainer9",
    TabsPanelContainer4: "TabsPanelContainer12",
    TabsPanelContainer5: "TabsPanelContainer13",
    TabsPanelContainer6: "TabsPanelContainer17",
    TabsPanelContainer8: "TabsPanelContainer20"
}


/**
 * 二级页签切换tab 映射 list
 */
const secondTabChangeList = {
    TabsPanelContainer10: "FormList3",
    TabsPanelContainer11: "FormList4",
    TabsPanelContainer12: "FormList5",
    TabsPanelContainer13: "FormList6",
    TabsPanelContainer14: "FormList7",
    TabsPanelContainer15: "FormList8",
    TabsPanelContainer17: "FormList9",
    TabsPanelContainer18: "FormList10",
    TabsPanelContainer19: "FormList11",
    TabsPanelContainer20: "FormList12"
}

//各列表查询条件组织id
const listQueryOrgIdItem = ["FormModelItem28", "FormModelItem29", "FormModelItem32", "FormModelItem38", "FormModelItem35",
    "FormModelItem41", "FormModelItem44", "FormModelItem47", "FormModelItem50", "FormModelItem53"];

//页面初始化
function myPageInit() {

    //给各列表查询条件组织id 赋值,从路由上去riskId
    listQueryOrgIdInit();

    //页签初始化位置
    h3ui.widgets.TabsContainer2.props.activeKey = 'TabsPanelContainer3';
    h3ui.widgets.TabsContainer3.props.activeKey = 'TabsPanelContainer9';


    //风控表单初始化
    orgRiskInfo();
}


/**
 * 触发列表刷新事件
 * @param {*} fromListId
 */
function reloadList(fromListId) {
    doReloadList({pageIndex: 1, node: {id: fromListId}});
}

//列表刷新
function doReloadList(evt) {
    var list = h3ui.widgets[evt.node.id];

    var queryId = list.childIds.find(a => a.includes("ModelQueryForm"));

    var filter = h3ui.widgets[queryId].props.value || {};

    filter = JSON.parse(JSON.stringify(filter));

    var pageId = list.childIds.find(a => a.includes("Pagination"));

    var pageIndex = h3ui.widgets[pageId].props.current;
    var pageSize = h3ui.widgets[pageId].props.pageSize;


    list.emit("load", {pageIndex, pageSize, filter});


    list.props.selectedRowKeys = [];
    list.props.selectedRow = [];

}

//删除事件
function deletes(evt) {
    console.log(evt);
    var keys = evt.data.keys;
    var schemaCode = evt.node.props.bindingModel.schemaCode;
    var listId = evt.node.id;
    if (keys.length < 1) {
        h3ui.$message.info("请勾选行数据");
        return;
    }
    h3ui.models[schemaCode].batchDeleteBO({bizObjectIds: keys}).then(() => {
        h3ui.$message.info('成功');
        // 刷新页面
        reloadList(listId);
    });
}


//所有表单都需要必填的字段
const fieldRequiredAll = ["name"];
//资质授权表单额外必填字段
const fieldRequiredFormList5 = ["effective", "expiration"];

/**
 * 必填字段只有两种情况
 * 关联性调查,经营活动,涉案诉讼,行政处罚,仲裁,内部反馈,官方焦点,社会舆论,审查列表 -> name
 * 资质授权(FormList5) -> name,effective,expiration
 */
function setfieldRequired(listId, dataSource) {
    dataSource.schema.properties.filter(a => fieldRequiredAll.includes(a.code)).forEach(a => a.required = true);

    dataSource.schema.properties.filter(a => fieldRequiredFormList5.includes(a.code))
        .forEach(a => {
            if (listId == "FormList5") {
                a.required = true;
            } else {
                a.required = false;
            }
        });
}

/**
 * listId 在配置里的字段就显示,否则不显示
 *
 *   所有情况下都显示的字段: name,description,annexAddress  不用控制
 *   资质授权额外字段 : effective,expiration,suggestion (["FormModelItem56","FormModelItem57","FormModelItem58"])
 *   涉案诉讼,行政处罚,仲裁,内部反馈,官方焦点,社会舆论,审查列表 额外字段: score (["FormModelItem59"])
 */
const visibleSetting = [
    {
        listId: ["FormList5"],
        code: ["FormModelItem56", "FormModelItem57", "FormModelItem58"]
    },
    {
        listId: ["FormList6", "FormList7", "FormList8", "FormList9", "FormList10", "FormList11", "FormList12"],
        code: ["FormModelItem59"]
    }
];


/**
 * 设置表单字段是否显示
 * @param { } listId
 */
function setVisible(listId) {
    visibleSetting.forEach(a => {
        var displayVal = a.listId.includes(listId) ? "" : "none";
        a.code.forEach(b => h3ui.widgets[b].props.visible.display = displayVal);
    })
}

// name 字段(FormModelItem13) label名称
const nameLabel = [
    {listId: ["FormList3"], name: '关联企业名称'},
    {listId: ["FormList4"], name: '活动名称'},
    {listId: ["FormList5"], name: '品牌名称'},
    {listId: ["FormList6", "FormList7", "FormList8"], name: '案件名称'},
    {listId: ["FormList9", "FormList10", "FormList11"], name: '标题'},
    {listId: ["FormList12"], name: '审查结果'}
]

/**
 * 设置 name 字段的 label 名称
 * @param {} listId
 */
function setNameLabel(listId) {
    var label = nameLabel.find(a => a.listId.includes(listId));

    h3ui.widgets["FormModelItem13-label"].props.content = label.name;
}

//风控信息表单数据初始化
function orgRiskInfo() {
    var dataSource = h3ui.models.t_organization_risk;
    var query = h3ui.$router.currentRoute.query;
    if (query.mode == 'detail' && query.riskId) {
        dataSource.subscribe('inited', function () {
            dataSource.loadBO({bizObjectId: query.riskId}).then(res => {
                console.log('风控信息表单数据初始化完成');
                // 清空监听：
                dataSource.subscribe('inited', function () {
                });
            });
        });
    }
}

//给各列表查询条件组织id 赋值,从路由上去riskId
function listQueryOrgIdInit() {
    var riskId = h3ui.$router.currentRoute.query.riskId;
    if (riskId) {
        listQueryOrgIdItem.forEach(a => h3ui.widgets[a].props.value = riskId);
    } else {
        h3ui.$message.error("页面路由数据错误!");
        history.back();
        return;
    }
}


//打开弹框关联信息
function optionButton(evt) {
    console.log(evt);
    var {code, name} = evt.data;
    //获取列表的id
    var listId = evt.node.id;

    var dataSource = h3ui.models.t_organization_info;
    setfieldRequired(listId, dataSource);//设置表单必填字段
    dataSource.newItem();

    //设置表单显示字段
    setVisible(listId);
    //设置 name 字段的 label 名称
    setNameLabel(listId);

    if ("openUpdate" == code) {//修改
        var rows = evt.node.props.selectedRow;
        if (rows.length != 1) {
            h3ui.$message.info("请选择且只能选择一行数据");
            return false;
        }
        setModifyOrgInfoMode(rows[0], dataSource);
    } else if ("openAdd" == code) {//新增
        setAddOrgInfoMode(listId, dataSource);
    } else {
        return;
    }

    var modal = h3ui.widgets.CustomModal1;
    modal.props.visible = true;
    modal.props.title = name;
    modal.props.listId = listId;

}


//设置新增关联信息默认值
function setAddOrgInfoMode(listId, dataSource) {

    var risk = h3ui.models.t_organization_risk.item;
    dataSource.item.org_id = risk.id;
    dataSource.item.org_name = risk.name;

    //通过列表id判断 类型
    var typeArr = orgInfoListType[listId];

    if (typeArr && typeArr.length == 2) {
        dataSource.item.type = typeArr[0];
        dataSource.item.sub_categories_type = typeArr[1];
    } else {
        throw new Error("打开新增弹框失败,组织关联信息类型获取失败,listId=" + listId);
    }
}


//设置修改关联信息
function setModifyOrgInfoMode(row, dataSource) {

    if (row.annex_address && row.annex_address.length > 0) {
        row.annex_address.forEach(b => {
            b.uid = b.id;
            b.status = "done";
            b.url = "/api/file/download?refId=" + b.id;
        })
    }
    dataSource.item = row;
}


//组织关联数据新增修改提交
function submitOrgInfo() {
    var modal = h3ui.widgets.CustomModal1;
    var action = modal.props.title;
    var submitService;
    if (action == "新增") {
        submitService = h3ui.models.t_organization_info.createBO;
    } else if (action == "修改") {
        submitService = h3ui.models.t_organization_info.updateBO;
    } else {
        h3ui.$message.warn("提交失败,action=" + action);
        return;
    }
    h3ui.$confirm({
        title: '确认提交吗',
        onOk() {
            submitService().then(res => {
                h3ui.$message.info("提交成功");
                reloadList(modal.props.listId);
                modal.props.visible = false;
            })
        }
    })

}

//一级页签切换事件
function firstTabChangeFun(evt) {
    console.log("一级页签切换事件", evt);
    var activeKey = evt.data.activeKey;
    var tabsPanelId = firstTabChangeList[activeKey];

    if (tabsPanelId) {
        var tabsPanel = h3ui.widgets[tabsPanelId];
        var tabsId = tabsPanel.parentId;
        h3ui.widgets[tabsId].props.activeKey = tabsPanelId;
        //激活二级页签
        secondTabChangeFun({'data': {'activeKey': tabsPanelId}});
    }

}


//二级页签切换事件
function secondTabChangeFun(evt) {
    console.log("二级页签切换事件", evt);
    var activeKey = evt.data.activeKey;
    var listId = secondTabChangeList[activeKey];

    if (listId) {
        //第一次不通过页签加载
        if (h3ui.widgets[listId].props.isFirstTabLoad == null) {
            h3ui.widgets[listId].props.isFirstTabLoad = false;
            return;
        } else {
            reloadList(listId);
        }

    }

}

